package com.starrin.tennisfinder.util;

import com.starrin.tennisfinder.model.BookingStatus;
import com.starrin.tennisfinder.model.HourAvailability;
import com.starrin.tennisfinder.model.VenueAvailabilityDataHolder;

import java.util.LinkedHashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by larsstarrin on 17/05/14.
 */
public class ExtractorUtil {

    public static Set<Integer> getAvailableHours(String patternString, String response) {
        Set<Integer> availableHours = new LinkedHashSet<>();
        Pattern pattern = Pattern.compile(patternString);
        Matcher matcher = pattern.matcher(response);

        while (matcher.find()) {
            String hour = matcher.group("hour");
            availableHours.add(Integer.valueOf(hour));
        }
        return availableHours;
    }

    public static VenueAvailabilityDataHolder getVenueAvailabilityDataHolder(String url, Set<Integer> availableHoursIndoors, Set<Integer> availableHoursOutdoors) {
        VenueAvailabilityDataHolder dataHolder = new VenueAvailabilityDataHolder();
        for(int i=7; i<23; i++) {
            if(availableHoursIndoors.contains(i) && availableHoursOutdoors.contains(i)) {
                dataHolder.addAvailability(new HourAvailability(i, BookingStatus.INDOORS_OUTDOORS, url));
            } else if(availableHoursIndoors.contains(i))  {
                dataHolder.addAvailability(new HourAvailability(i, BookingStatus.INDOORS, url));
            }  else if(availableHoursOutdoors.contains(i))  {
                dataHolder.addAvailability(new HourAvailability(i, BookingStatus.OUTDOORS, url));
            } else {
                dataHolder.addAvailability(new HourAvailability(i, BookingStatus.BOOKED, null));
            }
        }
        return dataHolder;
    }
}
